
package intelligent_maintenances

import (
	"context"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/intelligent_maintenances"
    intelligent_maintenancesReq "github.com/flipped-aurora/gin-vue-admin/server/model/intelligent_maintenances/request"
)

type IntelligentMaintenanceService struct {}
// CreateIntelligentMaintenance 创建智能保养记录
// Author [yourname](https://github.com/yourname)
func (intelligentMaintenanceService *IntelligentMaintenanceService) CreateIntelligentMaintenance(ctx context.Context, intelligentMaintenance *intelligent_maintenances.IntelligentMaintenance) (err error) {
	err = global.GVA_DB.Create(intelligentMaintenance).Error
	return err
}

// DeleteIntelligentMaintenance 删除智能保养记录
// Author [yourname](https://github.com/yourname)
func (intelligentMaintenanceService *IntelligentMaintenanceService)DeleteIntelligentMaintenance(ctx context.Context, ID string) (err error) {
	err = global.GVA_DB.Delete(&intelligent_maintenances.IntelligentMaintenance{},"id = ?",ID).Error
	return err
}

// DeleteIntelligentMaintenanceByIds 批量删除智能保养记录
// Author [yourname](https://github.com/yourname)
func (intelligentMaintenanceService *IntelligentMaintenanceService)DeleteIntelligentMaintenanceByIds(ctx context.Context, IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]intelligent_maintenances.IntelligentMaintenance{},"id in ?",IDs).Error
	return err
}

// UpdateIntelligentMaintenance 更新智能保养记录
// Author [yourname](https://github.com/yourname)
func (intelligentMaintenanceService *IntelligentMaintenanceService)UpdateIntelligentMaintenance(ctx context.Context, intelligentMaintenance intelligent_maintenances.IntelligentMaintenance) (err error) {
	err = global.GVA_DB.Model(&intelligent_maintenances.IntelligentMaintenance{}).Where("id = ?",intelligentMaintenance.ID).Updates(&intelligentMaintenance).Error
	return err
}

// GetIntelligentMaintenance 根据ID获取智能保养记录
// Author [yourname](https://github.com/yourname)
func (intelligentMaintenanceService *IntelligentMaintenanceService)GetIntelligentMaintenance(ctx context.Context, ID string) (intelligentMaintenance intelligent_maintenances.IntelligentMaintenance, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&intelligentMaintenance).Error
	return
}
// GetIntelligentMaintenanceInfoList 分页获取智能保养记录
// Author [yourname](https://github.com/yourname)
func (intelligentMaintenanceService *IntelligentMaintenanceService)GetIntelligentMaintenanceInfoList(ctx context.Context, info intelligent_maintenancesReq.IntelligentMaintenanceSearch) (list []intelligent_maintenances.IntelligentMaintenance, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&intelligent_maintenances.IntelligentMaintenance{})
    var intelligentMaintenances []intelligent_maintenances.IntelligentMaintenance
    // 如果有条件搜索 下方会自动创建搜索语句
    if len(info.CreatedAtRange) == 2 {
     db = db.Where("created_at BETWEEN ? AND ?", info.CreatedAtRange[0], info.CreatedAtRange[1])
    }
    
    if info.ServiceType != nil && *info.ServiceType != "" {
        db = db.Where("service_type = ?", *info.ServiceType)
    }
    if info.Status != nil && *info.Status != "" {
        db = db.Where("status = ?", *info.Status)
    }
    if info.Processor != nil && *info.Processor != "" {
        db = db.Where("processor LIKE ?", "%"+ *info.Processor+"%")
    }
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }
        var OrderStr string
        orderMap := make(map[string]bool)
           orderMap["ID"] = true
           orderMap["CreatedAt"] = true
         	orderMap["application_date"] = true
       if orderMap[info.Sort] {
          OrderStr = info.Sort
          if info.Order == "descending" {
             OrderStr = OrderStr + " desc"
          }
          db = db.Order(OrderStr)
       }

	if limit != 0 {
       db = db.Limit(limit).Offset(offset)
    }

	err = db.Find(&intelligentMaintenances).Error
	return  intelligentMaintenances, total, err
}
func (intelligentMaintenanceService *IntelligentMaintenanceService)GetIntelligentMaintenancePublic(ctx context.Context) {
    // 此方法为获取数据源定义的数据
    // 请自行实现
}
